window.BT = window.BT || {};
BT.Appdownload = {
    init : function(){
        var ua = navigator.userAgent.toLowerCase();
        var iOS = ( ua.match(/(ipad|iphone|ipod)/g) ? true : false );
        var appType = iOS ? 'ios' : 'android';
        //百度统计，下载按钮点击量
        _hmt.push(['_trackEvent', 'HTML5 banner', 'view', 'HTML5 banner 展示总量 ' + appType]);
        _hmt.push(['_trackEvent', 'HTML5 banner', 'view', $('.m-app-download').attr('data-type') + '页面 HTML5 banner 展示量 ' + appType]);
        $(".m-app-download").on("click", function(e) {
            e.preventDefault();
            var _url, _applink;
            var redirect = false;
            var _tripid = $(this).find('a').data("trip-id");
            var _userid = $(this).find('a').data("user-id");

            if (iOS) { _type = 'ios'; } else{ _type = 'android'; }
            if ( _tripid ) {
                _applink = '/download/?type='+ appType +'&src=mobile&trip_id=' + _tripid;
                _url = 'breadtrip://browsetrip?tripid=' + _tripid;
            }else if( _userid ){
                _applink = '/download/?type='+ appType +'&src=mobile&user_id=' + _userid;
                _url = 'breadtrip://userinfo?userid=' + _userid;
            }else{
                _applink = '/download/?type='+ appType +'&src=mobile';
                _url = iOS ?  'breadtrip://' : 'breadtrip://main';
            }
            //百度统计，下载按钮点击量
            _hmt.push(['_trackEvent', 'HTML5 banner', 'click', 'HTML5 banner 点击总量']);
            _hmt.push(['_trackEvent', 'HTML5 banner', 'click', 'HTML5 banner 点击量 ' + appType]);
            // 尝试跳转 App
            window.location = _url;
            // 跳转下载
            window.setTimeout(function() {
                redirect = true;
                //百度统计，下载按钮下载量(不准确)
                // _hmt.push(['_trackEvent',  appType + 'H5页面banner下载量', 'click']);
                window.location = _applink;
            }, 250);
            // window.setTimeout(function() {
            //     if (redirect) {
            //         window.location.reload();
            //     }
            // }, 500);
        });
    }
};